<!doctype html>
<html lang="en">
	<head>
		<title>three.js webgl - materials - cube reflection [Escher]</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#fff;
				color:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}
			a {	color: #ff0080;	text-decoration: none; }
			a:hover { color: #0080ff; }

			#d { text-align:center; margin:1em auto -9.0em; z-index:1000; position:relative; display:block;
				 background:rgba(0,0,0,0.75); padding:0.25em; width:300px; border-radius:10px; -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,0.5) }
			#oldie { margin-top:15em !important }
		</style>
	</head>

	<body>
		<div id="d">
			<p><a href="http://github.com/mrdoob/three.js">Three.js</a> cube mapping demo
			<p>Original artwork by <a href="http://en.wikipedia.org/wiki/Hand_with_Reflecting_Sphere" target="_blank">M. C. Escher</a>
			<p>Texture by <a href="http://brainwagon.org/2002/12/05/fun-with-environment-maps/" target="_blank">Mark VandeWettering</a>
		</div>

		<script src="../build/Three.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/Stats.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var statsEnabled = false;

			var container, stats;

			var camera, scene, renderer;

			var mesh, zmesh, lightMesh, geometry;

			var directionalLight, pointLight;

			var mouseX = 0;
			var mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 100000 );
				camera.position.z = 3200;
				scene.add( camera );

				var r = "textures/cube/Escher/";

				var urls = [ r + "px.jpg", r + "nx.jpg",
							 r + "py.jpg", r + "ny.jpg",
							 r + "pz.jpg", r + "nz.jpg" ];

				var textureCube = THREE.ImageUtils.loadTextureCube( urls );
				var material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } )
				var geometry = new THREE.SphereGeometry( 100, 96, 64 );

				var mesh = new THREE.Mesh( geometry, material );
				mesh.scale.x = mesh.scale.y = mesh.scale.z = 16;
				scene.add( mesh );

				// Skybox

				var shader = THREE.ShaderUtils.lib[ "cube" ];
				shader.uniforms[ "tCube" ].texture = textureCube;

				var material = new THREE.ShaderMaterial( {

					fragmentShader: shader.fragmentShader,
					vertexShader: shader.vertexShader,
					uniforms: shader.uniforms

				} ),

				mesh = new THREE.Mesh( new THREE.CubeGeometry( 6000, 6000, 6000 ), material );
				mesh.flipSided = true;
				scene.add( mesh );

				//

				renderer = new THREE.WebGLRenderer();
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				if ( statsEnabled ) {

					stats = new Stats();
					stats.domElement.style.position = 'absolute';
					stats.domElement.style.top = '0px';
					stats.domElement.style.zIndex = 100;
					container.appendChild( stats.domElement );

				}

			}

			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				if ( statsEnabled ) stats.update();

			}

			function render() {

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
